package com.projects.service.struct;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.miniserver.tql.database.DatabaseEngine;
import com.miniserver.tql.database.EngineContext;
import com.miniserver.tql.database.model.TNode;

public class Permission {
	/**
	 * web框架用于创建菜单树结构
	 * 
	 * @param engine
	 * @param context
	 * @param tNode
	 * @throws Exception
	 */
	public void editMenu(DatabaseEngine engine, EngineContext context,
			TNode tNode) throws Exception {
		try {

			String value = context.get("@return");

			JSONObject value_json = JSONObject.fromObject(value);
			JSONArray dataList = value_json.getJSONArray("data");
			JSONArray data = new JSONArray();
			for (int i = 0; i < dataList.size(); i++) {
				JSONObject p = dataList.getJSONObject(i);
				if (p.containsKey("parent")
						&& (p.get("parent").toString().equals("") || p
								.get("parent").toString().equals("0"))) {
					data.add(p);
				}
			}
			for (int i = 0; i < dataList.size(); i++) {
				JSONObject p = dataList.getJSONObject(i);
				if (p.containsKey("parent")
						&& (!p.get("parent").toString().equals("") && !p
								.get("parent").toString().equals("0"))) {
					String id = p.get("parent").toString();
					for (int j = 0; j < data.size(); j++) {
						if (data.getJSONObject(j).containsKey("id")
								&& data.getJSONObject(j).get("id").equals(id)) {
							JSONObject jobj = data.getJSONObject(j);
							if (jobj.containsKey("child")) {
								jobj.getJSONArray("child").add(p);
							} else {
								jobj.put("child", new JSONArray());
								jobj.getJSONArray("child").add(p);

							}
						}
					}
				}
			}
			value_json.put("data", data);
			context.put("@return", value_json.toString());
		} catch (Exception e) {
			e.printStackTrace();
			context.put("@return", "{\"success\": \"false\"}");
		}
	}

	/**
	 * web框架中用于创建权限树结构的
	 * 
	 * @param engine
	 * @param context
	 * @param tNode
	 * @throws Exception
	 */
	public void permissionTree(DatabaseEngine engine, EngineContext context,
			TNode tNode) throws Exception {
		try {
			String value = context.get("@return");

			JSONObject value_json = JSONObject.fromObject(value);
			JSONArray dataList = value_json.getJSONArray("data");
			JSONArray data = new JSONArray();
			for (int i = 0; i < dataList.size(); i++) {
				JSONObject p = dataList.getJSONObject(i);
				if (p.containsKey("level")
						&& "1".equals(p.get("level").toString())) {
					data.add(p);
				} else if (p.containsKey("level")
						&& "2".equals(p.get("level").toString())) {
					String id = p.getString("nav_id");
					for (int j = 0; j < data.size(); j++) {
						JSONObject nav = data.getJSONObject(j);
						String nav_id = nav.getString("menu_id");
						if (id != null && id.equals(nav_id)) {
							if (nav.containsKey("child")) {
								nav.getJSONArray("child").add(p);
							} else {
								nav.put("child", new JSONArray());
								nav.getJSONArray("child").add(p);
							}
						}
					}
				} else if (p.containsKey("level")
						&& "3".equals(p.get("level").toString())) {
					String p_nav_id = p.getString("nav_id");
					String p_parent = p.getString("parent");
					for (int j = 0; j < data.size(); j++) {
						JSONObject nav = data.getJSONObject(j);
						String nav_id = nav.getString("menu_id");
						if (p_nav_id != null && p_nav_id.equals(nav_id)) {
							JSONArray child = nav.getJSONArray("child");
							for (int k = 0; k < child.size(); k++) {
								JSONObject menu = child.getJSONObject(k);
								String parent = menu.getString("menu_id");
								if (p_parent != null && p_parent.equals(parent)) {
									if (menu.containsKey("child")) {
										menu.getJSONArray("child").add(p);
									} else {
										menu.put("child", new JSONArray());
										menu.getJSONArray("child").add(p);
									}
								}
							}
						}
					}
				}
			}

			value_json.put("data", data);
			context.put("@return", value_json.toString());
		} catch (Exception e) {
			e.printStackTrace();
			context.put("@return", "{\"success\": \"false\"}");
		}

	}

	public void tqlParentChild(DatabaseEngine engine, EngineContext context,
			TNode tNode) throws Exception {
		try {

			String value = context.get("@return");
			String cnt = context.get("@cnt");
			JSONObject value_json = JSONObject.fromObject(value);
			JSONArray dataList = value_json.getJSONArray("data");
			JSONArray data = new JSONArray();
			for (int i = 0; i < dataList.size(); i++) {
				JSONObject p = dataList.getJSONObject(i);
				if (p.containsKey("parent")
						&& (p.get("parent").toString().equals("") || p
								.get("parent").toString().equals("0"))) {
					data.add(p);
				}
			}
			for (int i = 0; i < dataList.size(); i++) {
				JSONObject p = dataList.getJSONObject(i);
				if (p.containsKey("parent")
						&& (!p.get("parent").toString().equals("") && !p
								.get("parent").toString().equals("0"))) {
					String id = p.get("parent").toString();
					for (int j = 0; j < data.size(); j++) {
						if (data.getJSONObject(j).containsKey("id")
								&& data.getJSONObject(j).get("id").equals(id)) {
							JSONObject jobj = data.getJSONObject(j);
							if (jobj.containsKey("child")) {
								jobj.getJSONArray("child").add(p);
							} else {
								jobj.put("child", new JSONArray());
								jobj.getJSONArray("child").add(p);

							}
						}
					}
				}
			}
			value_json.put("data", data);
			value_json.put("count", cnt);
			context.put("@return", value_json.toString());
		} catch (Exception e) {
			e.printStackTrace();
			context.put("@return", "{\"success\": \"false\"}");
		}
	}
	public void listToString(DatabaseEngine engine, EngineContext context,
			TNode tNode) throws Exception {
		try {
			
			String value = context.get("@id");
			JSONObject value_json = JSONObject.fromObject(value);
			JSONArray dataList = value_json.getJSONArray("data");
			StringBuffer str = new StringBuffer();
			for (int i = 0; i < dataList.size(); i++) {
				JSONObject obj = dataList.getJSONObject(i);
				str.append(obj.get("id") + ",");
			}
			context.put("@id", str.substring(0, str.length() - 1));
		} catch (Exception e) {
			e.printStackTrace();
			context.put("@return", "{\"success\": \"false\"}");
		}
	}
}
